import 'dart:async';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shop/enums/LoadingStatus.dart';
import 'package:shop/web/CommonWeb.dart';

//我要选购
class SelectBuyScreen extends StatefulWidget {
  @override
  SelectBuyScreenState createState() => SelectBuyScreenState();
}

class SelectBuyScreenState extends State<SelectBuyScreen> {
  var loadStatus = LoadingStatus.STATUS_IDLE;

  //定义页面加载的数据列表
  List<String> list = List();

  //定义ListView的滑动监听
  ScrollController scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    debugPrint("执行了方法initState");
    for (int i = 1; i <= 20; i++) {
      list.add("初始化显示数据$i");
    }
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        //加载下一页数据
        getMoreData();
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('我要选购'),
        centerTitle: true,
      ),
      body: RefreshIndicator(
          child: ListView.builder(
            itemCount: list.length + 1,
            itemBuilder: (BuildContext context, int index) {
              if (index == list.length) {
                //显示加载更多布局
                return loadingView();
              } else {
                return Column(
                  children: <Widget>[
                    GestureDetector(
                        child: Container(
                          width: MediaQuery.of(context).size.width,
                          child: padding(Text(list[index]),
                              top: 20.0, bottom: 20.0),
                        ),
                        onTap: () {
                          onItemClick(index, list[index]);
                        }),
                    Divider(height: 0.5)
                  ],
                );
              }
            },
            controller: scrollController,
          ),
          onRefresh: doRefresh),
    );
  }

  void onItemClick(int index, String item) {
    debugPrint("点击了第$index个item，内容是$item");
    Fluttertoast.showToast(msg: "点击了第$index个item");
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => CommonWeb("https://www.baidu.com", "")));
  }

  Widget paddingAll(Widget widget, var value) {
    return Padding(padding: EdgeInsets.all(value), child: widget);
  }

  Widget padding(Widget widget, {left, top, right, bottom}) {
    return Padding(
        padding: EdgeInsets.fromLTRB(
            left ??= 0.0, top ??= 0.0, right ??= 0.0, bottom ??= 0.0),
        child: widget);
  }

  Widget loadingView() {
    var loadingTS = TextStyle(color: Colors.blue, fontSize: 16);
    var loadingText = padding(Text("加载中...", style: loadingTS), left: 20.0);
    var loadingIndicator = Visibility(
        visible: loadStatus == LoadingStatus.STATUS_LOADING ? true : false,
        child: SizedBox(
          child: CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation(Colors.blue)),
          width: 20,
          height: 20,)
//          child: LinearProgressIndicator(
//            valueColor: AlwaysStoppedAnimation(Colors.blue),
//            value: 0.5,
//            backgroundColor: Colors.grey,
//          ),
        );

    return padding(
        Row(
          children: <Widget>[loadingIndicator, loadingText],
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
        ),
        top: 20.0,
        bottom: 20.0);
  }

  Future<void> doRefresh() async {
    await Future<Null>.delayed(Duration(microseconds: 2000), () {
      print("执行刷新动作");
      setState(() {
        loadStatus = LoadingStatus.STATUS_LOADING;
        list.clear();
        for (int i = 1; i <= 20; i++) {
          list.add("数据刷新后显示$i");
        }
      });
    });
  }

  Future<void> getMoreData() async {
    if (loadStatus == LoadingStatus.STATUS_IDLE) {
      //如果是正常
      setState(() {
        loadStatus = LoadingStatus.STATUS_LOADING;
      });
    }
    await Future<Null>.delayed(Duration(microseconds: 2000), () {
      print("执行加载跟多动作");
      List<String> moreList = List();
      for(int i = 0; i<= 20; i++){
        moreList.add("更多数据$i");
      }
      setState(() {
        if(list.length < 65){
          list.addAll(moreList);
          loadStatus = LoadingStatus.STATUS_IDLE;
        }else{
          loadStatus = LoadingStatus.STATUS_COMPLETED;
        }
      });
    });
  }
}
